
//Replication: McClintock, Cynthia. 2018. Electoral Rules and Democracy in
//Latin America. New York: Oxford University Press.

//Note: All results generated with Stata 14

***VARIABLE SETUP:
*Convert country to numeric
encode country, gen(pais)
xtset pais year

*Label Variables
rename ldi LDI
rename edi EDI
rename lldi lLDI
rename ledi lEDI
rename avg_ldi avg_LDI
label var fh "Freedom House"
label var fh_normal "Normalized FH Score"
label var enpp "Effective Number of Political Parties"
label var runoff "Runoff=1 Plurality=0"
label var yearsofrunoff "Number of Years since Runoff Rules Implemented"
label var yearsofplurality "Number of Years since Plurality Rules Implemented"
label var gdppc "GDP Per Capita"
label var agedem "Age of Democracy"
label var gini "GINI Coefficient"
label var giniadj "GINI Coefficient, Adjusted for Missing Observations"
label var primarycompletion "% Completed Primary Education"
label var primaryadj "% Completed Primary Education, Adjusted for Missing Observations"
label var LDI "V-DEM Liberal Democracy Index"
label var EDI "V-DEM Electoral Democracy Index"
label var adj_enpp "Effective Number of Political Parties, Adjusted for Missing Observations"
label var adj_enppq "Squared Effective Number of Political Parties, Adjusted for Missing Observations"
label var adj_enppc "Cubed Effective Number of Political Parties, Adjusted for Missing Observations"
label var lfh_normal "Lagged Normalized Freedom House Score"
label var lturnout "Lagged Electoral Turnout"
label var lLDI "Lagged LDI Index (V-DEM)"
label var lEDI "Lagged EDI Index (V-DEM)"


//Figure 2.1 (FH Scores) (p.20)
//Seting Scheme:
set scheme lean1 //To install lean1: ssc install gr0002
//Setup:
egen fh_normal_av_runoff = mean(fh_normal) if yearsofrunoff>0, by(year)
egen fh_normal_av_plurality = mean(fh_normal) if yearsofrunoff==0, by(year)
//Graphing (copy+paste one line at a time, omitting forward slashes at end):
 line  fh_normal_av_runoff fh_normal_av_plurality year, sort connect(l) ///
 ytitle("Normalized Score") ///
 xtitle("Year") ///
 xlabel(1990(2)2016, angle(90)) ylabel(40(10)80) ///
 plotr(margin(b=0 t=0 l=0 r=0)) ///
 legend(pos(6) row(2) label(1 "Average score with runoff rules") ///
 label(2 "Average score with plurality rules"))


//Figure 2.2 (LDI Scores) (p.20)
//Seting Scheme:
set scheme lean1 //To install lean1: ssc install gr0002; may require updates
//Setup:
egen LDI_av_runoff = mean(LDI) if yearsofrunoff>0, by(year)
egen LDI_av_plurality = mean(LDI) if yearsofrunoff==0, by(year)
//Graphing (copy+paste one line at a time; omit forward slashes):
line  LDI_av_runoff LDI_av_plurality year, sort connect(l) ///
ytitle("LDI Score") ///
xtitle("Year") ///
xlabel(1990(2)2016, angle(90)) ylabel(.35(.1).65) ///
plotr(margin(b=0 t=0 l=0 r=0)) ///
legend(pos(6) row(2) label(1 "Average score with runoff rules") ///
label(2 "Average score with plurality rules"))

 
//Figure 2.3 (Turnout) (p.21)
//Seting Scheme:
set scheme lean1
//Graphing (copy+paste one line at a time; omit forward slashes):
twoway scatter  turnout year if runoff==1, msymbol(none) || ///
lfit turnout year if runoff==1, lpattern(l) || ///
scatter turnout year if runoff==0, msymbol(none) || ///
lfit turnout year if runoff==0, lpattern(-) ///
yti("Voter Turnout" "Percentage of Registered Voters") xti("Year") ///
legend(pos(6) row (2) label(1 "Average turnout with runoff rules") ///
label(2 "Average turnout with plurality rules")) ///
xlab(1990(2)2016, angle (90)) ylab(30(10)100) ///
plotr(margin(b=0 t=0 l=0 r=0))


//Table 2.2 (p.24)
*Freedom House
xtreg fh_normal runoff agedem gdppc gdpgrowth giniadj primaryadj adj_ENPP adj_ENPPq lfh_normal 
*VDEM Liberal Democracy Index
clonevar scaled_gdppc=gdppc
replace scaled_gdppc=scaled_gdppc*.0000001
xtreg LDI runoff agedem scaled_gdppc gdpgrowth giniadj primaryadj adj_ENPP adj_ENPPq lLDI

//Table 2.3 (p.25)
*Turnout
xtreg turnout runoff agedem gdppc gdpgrowth giniadj primaryadj adj_ENPP adj_ENPPq lturnout

//Table 2.4 (p.26)
*Freedom House
xtreg fh_normal yearsofplurality agedem gdppc gdpgrowth giniadj primaryadj adj_ENPP adj_ENPPq lfh_normal
*VDEM Liberal Democracy Index
xtreg LDI yearsofplurality agedem gdppc gdpgrowth giniadj primaryadj adj_ENPP adj_ENPPq lLDI

//Figure 3.1 (p.31)
//Seting Scheme:
set scheme lean1
//Setup:
egen adj_ENPP_av_runoff = mean(adj_ENPP) if runoff==1, by(year)
egen adj_ENPP_av_plurality = mean(adj_ENPP) if runoff==0, by(year)
//Graphing:
line  adj_ENPP_av_runoff adj_ENPP_av_plurality year, sort connect(l) ///
 ytitle("Number of Parties") ///
 xlabel(1990(2)2016, angle (90)) ylabel(2(1)6) ///
 plotr(margin(b=0 t=0 l=0 r=0)) ///
 legend(pos(6) row(2) label(1 "Average number of parties with runoff rules") ///
 label(2 "Average number of parties with plurality rules"))
 
//Figure 3.2 (p.33)
//Setup:
clonevar adj_ENPP_rounded=adj_ENPP
replace  adj_ENPP_rounded=1 if adj_ENPP_rounded<1.45
replace  adj_ENPP_rounded=2 if adj_ENPP_rounded>=1.45 & adj_ENPP_rounded<2.45
replace  adj_ENPP_rounded=3 if adj_ENPP_rounded>=2.45 & adj_ENPP_rounded<3.45
replace  adj_ENPP_rounded=4 if adj_ENPP_rounded>=3.45 & adj_ENPP_rounded<4.45
replace  adj_ENPP_rounded=5 if adj_ENPP_rounded>=4.45 & adj_ENPP_rounded<5.45
replace  adj_ENPP_rounded=6 if adj_ENPP_rounded>=5.45 & adj_ENPP_rounded<6.45
replace  adj_ENPP_rounded=7 if adj_ENPP_rounded>=6.45 & adj_ENPP_rounded<7.45
replace  adj_ENPP_rounded=8 if adj_ENPP_rounded>=7.45 & adj_ENPP_rounded<8.45
replace  adj_ENPP_rounded=9 if adj_ENPP_rounded>=8.45 & adj_ENPP_rounded<9.45
replace  adj_ENPP_rounded=10 if adj_ENPP_rounded>=9.45 & adj_ENPP_rounded<10.45
replace  adj_ENPP_rounded=11 if adj_ENPP_rounded>=10.45 & adj_ENPP_rounded<11.45
replace  adj_ENPP_rounded=12 if adj_ENPP_rounded>=11.45 & adj_ENPP_rounded<12.45
egen avg_LDI=mean(LDI), by (adj_ENPP_rounded)
//Graphing:
line  avg_LDI adj_ENPP_rounded, sort connect(l) ///
 ytitle("Avg. Liberal Democracy Index") xtitle("ENPP") ///
 xlabel(1(1)12, angle (90)) ylabel(.1(.1).8) ///
  plotr(margin(b=0 t=0 l=0 r=0))



